package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.CaseDao;
import com.pojo.CasePojo;
import com.util.Log;

@WebServlet(urlPatterns={"/admin/SubmitCase"})
public class SubmitCase extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		String caseName = request.getParameter("caseName");
		String content = request.getParameter("content");
		String message = "案例添加失败";
		
		if(!(caseName.equals("")) && !(content.equals(""))) {
			CaseDao dao = new CaseDao();
			CasePojo pojo = new CasePojo();
			
			pojo.setCaseName(caseName);
			pojo.setContent(content);
			Timestamp publishDate = new java.sql.Timestamp(new java.util.Date().getTime());
			pojo.setPublishDate(publishDate);
			pojo.setPublishStatus(0);
			
			if (dao.addCase(pojo) > 0)
				message = "添加案例成功";
			dao.closeAll();
		} else {
			message = "添加案例的请求参数错误";
		}
		
		String json = "{\"status\":\"" + message + "\"}";
		Log.write(json);
		out.println(json);
		out.flush();
		out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
